package com.hw.shortlink.admin.remote.dto;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hw.shortlink.admin.common.convention.response.Result;
import com.hw.shortlink.admin.common.convention.response.Results;
import com.hw.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.hw.shortlink.admin.remote.dto.req.ShortLinkPageReqDto;
import com.hw.shortlink.admin.remote.dto.req.ShortLinkUpdateReqDto;
import com.hw.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDto;
import com.hw.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDto;
import com.hw.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ShortLinkRemoteService {


    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDto shortLinkPageReqDto) {

        Map<String, Object> map = new HashMap<>();
        map.put("gid",shortLinkPageReqDto.getGid());
        map.put("current",shortLinkPageReqDto.getCurrent());
        map.put("size",shortLinkPageReqDto.getSize());
        String json = HttpUtil.get("http://localhost:8001/api/short-link/v1/page", map);
        return JSON.parseObject(json, new TypeReference<>() {
        });
    }

    default Result<ShortLinkCreateRespDto> createShortLink(@RequestBody ShortLinkCreateReqDTO shortLinkCreateReqDTO) {

        //todo 调用有误，貌似和字符集有关，然后beanToMap这个方法也不能用
        Map<String, Object> map = BeanUtil.beanToMap(shortLinkCreateReqDTO);
        String json = HttpUtil.post("http://localhost:8001/api/short-link/v1/create", map);
        return JSON.parseObject(json, new TypeReference<>() {
        });
    }


    default Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDto shortLinkUpdateReqDTO) {

        //todo 调用有误，貌似和字符集有关，然后beanToMap这个方法也不能用
        Map<String, Object> map = BeanUtil.beanToMap(shortLinkUpdateReqDTO);
        HttpUtil.post("http://localhost:8001/api/short-link/v1/update", map);
        return Results.success();
    }


    default Result<List<ShortLinkGroupCountQueryRespDto>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> gids) {

        Map<String,Object> map=new HashMap<>();

        map.put("requestParam",gids);

        String json = HttpUtil.get("http://localhost:8001/api/short-link/v1/count",map);

        return JSON.parseObject(json, new TypeReference<>() {
        });
    }


}
